package com.qianbei.user.applymaster;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.android.volley.Request.Method;
import com.qianbei.R;
import com.qianbei.common.base.BaseActivity;
import com.qianbei.common.init.GlobalConfig;
import com.qianbei.common.net.control.Constant;
import com.qianbei.common.net.control.ServerResult;
import com.qianbei.common.net.view.MsgAlert;
import com.qianbei.common.web.WebActivity;

/**
 * 申请成为前辈 页面 下一步调到资料填写页面
 * 
 * @author gml
 * 
 */
public class ApplyQianbeiActivity extends BaseActivity {

	
	private TextView apply_add ;
	private boolean isClick;
	private Dialog dialog;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.apply_qianbei_activity);
		setTitle(" 申请成为前辈");
		apply_add =(TextView) findViewById(R.id.apply_add);
		apply_add.setOnClickListener(this);

		applyRequest();
	}
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()){
			case R.id.apply_add:
				applyRequest();
				isClick = true;
				break;
			case R.id.phone_btn:  //dialog 手机申请前辈
				dismiss();
				break;
			case R.id.computer_btn: //dialog 电脑申请前辈
				WebActivity.startWebActivity(mContext, Constant.APPLY_COMPUTER_QIAN_BEI_URL);
				dismiss();
				break;
			case R.id.layout_close:	//dialog 关闭按钮
				dismiss();
				break;
			case R.id.pop_layout: //dialog 布局
				dismiss();
				break;
		}

	}

	public void applyRequest(){
		if ("1".equals(GlobalConfig.getInstance().identity)){
			new MsgAlert().show(R.string.apply_qian_bei);
		}else{
			initAlertServer(Method.POST, user_seniority_info);
		}
	}


	@Override
	public void serverFinish(ServerResult sr) {

		if (sr.isContinue) {
			boolean canapply=sr.bodyData.optBoolean("can_apply_master");
			if(canapply && isClick){
				showDialog();
			}else if(canapply){
				
			}else {
				apply_add.setText("审核中，请等待");
				new MsgAlert().show("审核中，两个工作日内会得到答复！");
			}
		}
	}


	public void showDialog(){
		if (dialog == null) {
			dialog = new Dialog(this, R.style.custom_dialog);
			dialog.setCanceledOnTouchOutside(true);
			//获取对话框的窗口，并设置窗口参数
			Window win = dialog.getWindow();
			WindowManager.LayoutParams params = new WindowManager.LayoutParams();
			// 不能写成这样,否则Dialog显示不出来
			// LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
			//对话框窗口的宽和高
			params.width = WindowManager.LayoutParams.MATCH_PARENT;
			params.height = WindowManager.LayoutParams.WRAP_CONTENT;
			win.setAttributes(params);
			//设置对话框布局
			dialog.setContentView(R.layout.pop_apply_bottom);
			//获取对话框布局中的控件，并设置事件监听
			dialog.findViewById(R.id.computer_btn).setOnClickListener(this);
			dialog.findViewById(R.id.phone_btn).setOnClickListener(this);
			dialog.findViewById(R.id.layout_close).setOnClickListener(this);
			dialog.findViewById(R.id.pop_layout).setOnClickListener(this);
		}
		dialog.show();
	}

	/**
	 * 隐藏对话框
	 */
	public void dismiss() {
		//隐藏对话框之前先判断对话框是否存在，以及是否正在显示
		if (dialog != null && dialog.isShowing()) {
			dialog.dismiss();
		}
	}

}
